//
//  SuggestionViewController.m
//  Finder
//
//  Created by cdm on 11-3-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SuggestionViewController.h"
#import "WebBrowserController.h"
#import "DeviceUtil.h"
#import "RemoteService.h"

@interface SuggestionViewController () 

- (void)hideKeyboard:(id)sender;
- (void)appLanguageDidChange;
- (BOOL)validateForm;

@end

@implementation SuggestionViewController

@synthesize mailLabel;
@synthesize contentLabel;
@synthesize faxLabel;
@synthesize mailTextField;
@synthesize contentTextView;
@synthesize submitButton;
@synthesize phoneButton;
@synthesize emailButton;
@synthesize tweetButton;
@synthesize backgroundButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [self.mailLabel release];
    [self.contentLabel release];
    [self.faxLabel release];
    [self.mailTextField release];
    [self.contentTextView release];
    [self.submitButton release];
    [self.phoneButton release];
    [self.emailButton release];
    [self.tweetButton release];
    [self.backgroundButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.mailLabel.text = [AppLanguage getString:@"Email"];
    self.contentLabel.text = [AppLanguage getString:@"Suggestion"];
    [self.submitButton setTitle:[AppLanguage getString:@"Submit"] forState:UIControlStateNormal];
    [self.phoneButton setTitle:[AppLanguage getString:@"CorpPhone"] forState:UIControlStateNormal];
    [self.emailButton setTitle: [AppLanguage getString:@"CorpEmail"] forState:UIControlStateNormal];
    [self.tweetButton setTitle:[AppLanguage getString:@"CorpTweet"] forState:UIControlStateNormal];
    self.faxLabel.text = [AppLanguage getString:@"CorpFax"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appLanguageDidChange) name:kAppLanguageChangedNotification object:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.mailLabel = nil;
    self.contentLabel = nil;
    self.faxLabel = nil;
    self.mailTextField = nil;
    self.contentTextView = nil;
    self.submitButton = nil;
    self.phoneButton = nil;
    self.emailButton = nil;
    self.tweetButton = nil;
    self.backgroundButton = nil;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.parentViewController.navigationItem.title =self.title;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Event

- (void)hideKeyboard:(id)sender
{
    [self.mailTextField endEditing:YES];
    [self.contentTextView endEditing:YES];
}

- (IBAction)submitButtonOnClick:(id)sender
{
    if ([self validateForm]) {
        
        NSString *feedbackEmail = self.mailTextField.text;
        NSString *content = self.contentTextView.text;
        
        BOOL result = [[RemoteService sharedService] sendEmail:feedbackEmail content:content];
        
        if (result) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提交成功" message:@"感谢您提供宝贵意见" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"发送成功" message:@"发送失败，请稍后重试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            [alertView release];
            
        }

    }
}

- (IBAction)phoneButtonOnClick:(id)sender
{
    NSString *phoneNumber = @"4007361098";
    if ([DeviceUtil canCallPhone]) {
        NSString *tel = [@"tel:" stringByAppendingString:phoneNumber];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
    } else {
        NSString *message = [NSString stringWithFormat:@"您使用的设备无法拨打电话，请用手机拨打:%@", phoneNumber];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        [alertView release];
    }
}

- (IBAction)emailButtonOnClick:(id)sender
{
    if ([DeviceUtil canSendMail]) {
        MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
        mailController.mailComposeDelegate = self;
        [mailController setToRecipients:[NSArray arrayWithObject:@"service@bw30.com"]];
        [self presentModalViewController:mailController animated:YES];
        [mailController release];
    } else {
        NSString *message = [NSString stringWithFormat:@"您使用的设备无法发送邮件，请用邮箱发送到:%@", @"service@bw30.com"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        [alertView release];
    }
}

- (IBAction)tweetButtonOnClick:(id)sender
{
    WebBrowserController *webBrowserController = [[WebBrowserController alloc] initWithNibName:@"WebBrowser" bundle:nil];
    [webBrowserController loadWithURL:@"http://t.sina.com.cn/lxapp"];
    [self presentModalViewController:webBrowserController animated:YES];
    [webBrowserController release];
}

- (IBAction)backgroundButtonOnClick:(id)sender
{
    [self hideKeyboard:sender];
}

#pragma mark - MFMail compose deleate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - App Language

- (void)appLanguageDidChange
{
    self.mailLabel.text = [AppLanguage getString:@"Email"];
    self.contentLabel.text = [AppLanguage getString:@"Suggestion"];
    [self.submitButton setTitle:[AppLanguage getString:@"Submit"] forState:UIControlStateNormal];
    [self.phoneButton setTitle:[AppLanguage getString:@"CorpPhone"] forState:UIControlStateNormal];
    [self.emailButton setTitle: [AppLanguage getString:@"CorpEmail"] forState:UIControlStateNormal];
    [self.tweetButton setTitle:[AppLanguage getString:@"CorpTweet"] forState:UIControlStateNormal];
    self.faxLabel.text = [AppLanguage getString:@"CorpFax"];
}

#pragma mark - Validate

- (BOOL)validateForm
{
    NSString *msg = @"";
    if ([mailTextField.text isEqualToString:@""]) {
        msg = [msg stringByAppendingString:@"请输入邮箱\r\n"];
    }
    
    if ([contentTextView.text isEqualToString:@""]) {
        msg = [msg stringByAppendingString:@"请输入意见\r\n"];
    }
    
    if ([msg length] > 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提交失败" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        [alertView release];
        
        return NO;
    } else {
        return YES;
    }
}

@end

